﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace Dycox.Validation
{
    [AttributeUsage(AttributeTargets.Property, AllowMultiple =true)]
    public class CompareValidatorAttribute : ValidatorAttribute
    {

        public CompareValidatorAttribute(CompareOperator op, int value)
            : this(op, (IComparable)value)
        {

        }

        public CompareValidatorAttribute(CompareOperator op, string value)
            : this(op, (IComparable)value)
        {
        }

        public CompareValidatorAttribute(CompareOperator op, string value, Type valueType)
            : this(op, (IComparable)Dycox.Utils.ConvertUtil.ConvertType(value, valueType))
        {

        }

        public CompareValidatorAttribute(bool value)
            : this(value ? CompareOperator.Equals : CompareOperator.NotEquals, true)
        {

        }

        public CompareValidatorAttribute(CompareOperator op, Type type)
            : base(op, type)
        {
            
        }

        private CompareValidatorAttribute(CompareOperator op, IComparable value)
            : base(op, value)
        {

        }

        protected override Validator CreateValidator(object[] parameters, ITypeDescriptorContext context)
        {
            CompareValidator val = new CompareValidator();
            val.Operator = (CompareOperator)parameters[0];
            val.ValueToCompare = Dycox.Utils.ConvertUtil.ConvertType(parameters[1], context);
            return val;
        }
    }
}
